package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ViewStudentPPF extends Activity implements OnClickListener {
	
	EditText id;
	Button submit;
	String studID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_student_ppf);
		
		id = (EditText) findViewById(R.id.edittxtStudNo);
		submit = (Button) findViewById(R.id.search);
		submit.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.view_student_p, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v== submit){
			
			studID = id.getText().toString();
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/pagol15/www/supervisor_mppf.asp?sid="+studID);
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("proTitle","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[4]/td[2]---");
			xpathMap.put("systemType","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[6]/td[2]---");
			xpathMap.put("developedFor","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[8]/td[2]---");
			xpathMap.put("requiredFor","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[10]/td[2]---");
			xpathMap.put("challenges","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[12]/td[2]---");
			xpathMap.put("newIdeas","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[14]/td[2]---");

			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			
			BPEL_Response respones = null;

			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String proTitle = respones.getResultsMap().get("proTitle").trim();
			String systemType = respones.getResultsMap().get("systemType").trim();
			String developedFor = respones.getResultsMap().get("developedFor").trim();
			String requiredFor = respones.getResultsMap().get("requiredFor").trim();
			String challenges = respones.getResultsMap().get("challenges").trim();
			String newIdeas = respones.getResultsMap().get("newIdeas").trim();

			
			Toast.makeText(getApplicationContext(), proTitle,
					Toast.LENGTH_LONG).show();
			
			Intent i = new Intent(this, ViewPPF.class);			
			i.putExtra("proTitle", proTitle);
			i.putExtra("systemType", systemType);
			i.putExtra("developedFor", developedFor);
			i.putExtra("requiredFor", requiredFor);
			i.putExtra("challenges", challenges);
			i.putExtra("newIdeas", newIdeas);

			startActivity(i);
			
		}
	}

}
